﻿using XKJ.OAWeb.Application;
using XKJ.OAWeb.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using XKJ.OAWeb.Shared;
using XKJ.OAWeb.Api.Filters;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using System.IdentityModel.Tokens.Jwt;
using XKJ.OAWeb.Infrastructrue.Helpers.JWT;

namespace XKJ.OAWeb.Api.Controllers
{
    /// <summary>
    /// 用户模块
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    //授权特性，表示：下面的所有方法都必须登录才能调用
    [Authorize]
    public class UserController : ControllerBase
    {
        IUserService _userService;

        JWTHelper _helper;

        //日志(GetCurrentClassLogger())
        NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();

        public UserController(IUserService userService, JWTHelper helper)
        {
            _userService = userService;
            _helper = helper;
        }
        /// <summary>
        /// 登录验证
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [NoLog]
        [HttpPost]
        [ResponseHeader("xxx", "yyy")]
        //允许匿名访问的特性
        [AllowAnonymous]
        public IActionResult Login(User user)
        {
            try
            {
                //模型验证
                if (!ModelState.IsValid)
                {
                    return BadRequest();//返回400状态
                }

                var result = _userService.Login(user);

                if (result == null)
                {
                    return Ok(new UserData
                    {
                        Code = -1,
                        Msg = "登录不成功",
                        Data = null
                    });
                }
                //写入日志
                logger.Error(200);



                //荷载信息
                List<Claim> claims = new List<Claim>()
                {
                    new Claim(ClaimTypes.Name,user.UserName),
                    new Claim(ClaimTypes.NameIdentifier,user.UId.ToString()),
                };

                return Ok(new
                {
                    Code = 0,
                    Msg = "登录成功",
                    Dara=user,
                    Token= _helper.CreateToken(claims)
                });

                //return Ok(new UserData
                //{
                //    Code = 0,
                //    Msg = "登录成功",
                //    Data = result
                //});
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
            }
            return Ok();
        }
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="Pageindex"></param>
        /// <param name="Pagesize"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet]
        //允许匿名访问的特性
        [AllowAnonymous]
        public IActionResult UserShow(int Pageindex, int Pagesize,string? UserName)
        {
            return Ok(_userService.UserShow(Pageindex, Pagesize, UserName));
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        //允许匿名访问的特性
        [AllowAnonymous]
        public IActionResult UserAdd(User user)
        {
            return Ok(_userService.UserAdd(user));
        }
        /// <summary>
        /// 根据Id删除
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        //允许匿名访问的特性
        [AllowAnonymous]
        public IActionResult UserDeleteId(int Id)
        {
            return Ok(_userService.UserDeleteId(Id));
        }
        /// <summary>
        /// 根据Id反填数据
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [HttpGet]
        //允许匿名访问的特性
        [AllowAnonymous]
        public IActionResult UserBackfill(int Id)
        {
            return Ok(_userService.UserBackfill(Id));
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        //允许匿名访问的特性
        [AllowAnonymous]
        public IActionResult UserUptdate(User user)
        {
            return Ok(_userService.UserUptdate(user));
        }
        /// <summary>
        /// 用户角色显示
        /// </summary>
        /// <param name="Pageindex"></param>
        /// <param name="Pagesize"></param>
        /// <param name="UserName"></param>
        /// <returns></returns>
        [HttpGet]
        //允许匿名访问的特性
        [AllowAnonymous]
        public IActionResult UserRoleShow(int Pageindex, int Pagesize, string? UserName)
        {
            return Ok(_userService.UserRoleShow(Pageindex, Pagesize, UserName));
        }
    }
}
